var $ = require('../../common/helper.js');
Page({
	data: {
		renew: 0,
		person_id: '',
		province_id: '',
		city_id: '',
		casy_id: '',
		month: '',
		gjj: '',
		person: '请选择参保人',
		city: '请选择参保地',
		casy: '请选择参保方案',
		months: '请选择参保月份',
		person_text: '',
		city_text: '',
		casy_text: '',
		month_text: '',
		tip: [
			'同一个身份证号码，同一个城市，只能按照初次购买的社保、公积金方案续费，不能修改社保公积金方案续费。',
			'不承接现已怀孕、重病等特殊人员的社保代缴业务。如客户隐瞒孕情、病情下单，我司无法为其办理相应的社保待遇。如有生育计划，请在孕前3个月，即生育前13个月提前购买社保。',
			'关于生育待遇的申领，我司仅承接异地分娩，生育备案，医疗报销等业务。涉及生育津贴、产假工资的申领我司概不接受。',
			'首次支付时系统会提示上传身份证正反面材料，填写好个人信息支付订单后，我司会有人跟进，如后续需要有其它材料补充，我司会再联系您。'
		]
	},
	onLoad: function (options) {
		$.overload(false);
		var _this = this;
		if (typeof (options.renew) != 'undefined') {
			this.setData({
				renew: parseInt(options.renew)
			});
		}
		if (typeof (options.renew) != 'undefined' && typeof (options.city_id) != 'undefined' && typeof (options.person_id) != 'undefined') {
			$.get('api.php?app=cart&act=get_last', {
				city_id: options.city_id,
				person_id: options.person_id
			}, function (json) {
				if (json.data) {
					_this.setData({
						casy_id: json.data.casy_id,
						casy: json.data.casy_name,
						casy_text: 'text-x',
						gjj: parseFloat(json.data.gjj) > 0 ? json.data.gjj : ''
					});
				}
			});
		}
		if (typeof (options.province_id) != 'undefined' && typeof (options.city_id) != 'undefined' && typeof (options.casy_id) != 'undefined') {
			$.get('api.php?app=cart&act=info', options, function (json) {
				var str = json.data.city_name.replace('市', '') + '地区的社保增员截止日期为每个月的' + json.data.end_day + '号，超过此日期将不能购买当月，老客户将自动减员。';
				var tip = _this.data.tip;
				tip.unshift(str);
				var person_text = 'text-x';
				if (typeof (options.person_id) == 'undefined') {
					options.person_id = '';
					json.data.person = '';
					person_text = '';
				}
				_this.setData({
					person_id: options.person_id,
					province_id: options.province_id,
					city_id: options.city_id,
					casy_id: options.casy_id,
					person: json.data.person,
					city: json.data.city,
					casy: json.data.casy,
					person_text: person_text,
					city_text: 'text-x',
					casy_text: 'text-x',
					tip: tip
				});
			});
		}
	},
	checkRenew: function (e) {
		if (this.data.renew == 1) return false;
		if (typeof e == 'undefined') return true;
		wx.navigateTo({
			url: e.currentTarget.dataset.url,
		});
	},
	selectProvince: function () {
		if (!this.checkRenew()) return;
		console.log(this.data.person_id);
		if (!this.data.person_id.length) {
			$.overloadError('请先选择参保人');
			return;
		}
		wx.navigateTo({
			url: '/pages/cart/casy?city_id=' + this.data.city_id,
		});
	},
	selectCasy: function () {
		if (!this.checkRenew()) return;
		if (!this.data.person_id.length) {
			$.overloadError('请先选择参保人');
			return;
		}
		if (!this.data.city_id.length) {
			$.overloadError('请先选择参保地');
			return;
		}
		wx.navigateTo({
			url: '/pages/cart/casy?city_id=' + this.data.city_id + '&person_id=' + this.data.person_id,
		});
	},
	selectMonth: function () {
		if (!this.data.person_id.length || !this.data.city_id.length) {
			$.overloadError('请先选择参保人与参保地');
			return;
		}
		wx.navigateTo({
			url: '/pages/cart/month?city_id=' + this.data.city_id + '&person_id=' + this.data.person_id,
		});
	},
	setParam: function (type, data) {
		var _this = this;
		switch (type) {
			case 'person':
				this.setData({
					person_id: data.person_id,
					person: data.name,
					person_text: 'text-x'
				});
				if (this.data.city_id.length) {
					$.get('api.php?app=cart&act=get_last', {
						city_id: this.data.city_id,
						person_id: data.person_id
					}, function (json) {
						if (json.data) {
							_this.setData({
								casy_id: json.data.casy_id,
								casy: json.data.casy_name,
								casy_text: 'text-x',
								gjj: parseFloat(json.data.gjj) > 0 ? json.data.gjj : ''
							});
						}
					});
				}
				break;
			case 'city':
				var str = data.city.replace('市', '') + '地区的社保增员截止日期为每个月的' + data.end_day + '号，超过此日期将不能购买当月，老客户将自动减员。';
				var tip = this.data.tip;
				tip.unshift(str);
				this.setData({
					province_id: data.province_id,
					city_id: data.city_id,
					city: data.name,
					city_text: 'text-x',
					tip: tip
				});
				var _this = this;
				$.get('api.php?app=cart&act=casy&city_id=' + data.city_id, function (json) {
					if (!$.isArray(json.data.casy) || !json.data.casy.length) return;
					var casy = json.data.casy,
						row = json.data.row;
					_this.setData({
						casy_id: casy[0].id,
						casy: casy[0].name,
						gjj: row.money,
						casy_text: 'text-x'
					});
				});
				if (this.data.person_id.length) {
					$.get('api.php?app=cart&act=get_last', {
						city_id: data.city_id,
						person_id: this.data.person_id
					}, function (json) {
						if (json.data) {
							_this.setData({
								casy_id: json.data.casy_id,
								casy: json.data.casy_name,
								casy_text: 'text-x',
								gjj: parseFloat(json.data.gjj) > 0 ? json.data.gjj : ''
							});
						}
					});
					$.get('api.php?app=cart&act=month&city_id=' + data.city_id + '&person_id=' + this.data.person_id, function (json) {
						if (!$.isArray(json.data.years) || !json.data.years.length) return;
						var years = json.data.years,
							month = [];
						for (var i = 0; i < json.data.min_months; i++) {
							month.push(years[i].year + '-' + years[i].month);
						}
						var html = month[0];
						if (month.length > 1) html = month[0] + ' ~ ' + month[month.length - 1];
						_this.setData({
							month: month.join(','),
							months: html,
							month_text: 'text-x'
						});
					});
				}
				break;
			case 'casy':
				this.setData({
					casy_id: data.casy_id,
					casy: data.name,
					gjj: data.gjj,
					casy_text: 'text-x'
				});
				break;
			case 'month':
				var name = '';
				var name = data[0];
				if (data.length > 1) name = data[0] + ' ~ ' + data[data.length - 1];
				this.setData({
					month: data.join(','),
					months: name,
					month_text: 'text-x'
				});
				break;
		}
	},
	postForm: function (e) {
		var data = e.detail.value;
		if (!data.person_id.length) {
			$.overloadError('请选择参保人');
			return;
		}
		if (!data.province_id.length || !data.city_id.length) {
			$.overloadError('请选择参保地');
			return;
		}
		if (!data.casy_id.length) {
			$.overloadError('请选择参保方案');
			return;
		}
		if (!data.month.length) {
			$.overloadError('请选择参保月份');
			return;
		}
		wx.navigateTo({
			url: '/pages/cart/jiesuan?' + $.paramUrl(data),
		});
	},
	onShareAppMessage: function (res) {
		return {
			title: $.config.shareTitle,
			imageUrl: $.config.shareImageUrl,
			path: this.route
		}
	}
})